package com.ang.jdk8.stream;

import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SumList {

    @Data
    class User{
        private int id;
        private String name;
        private int age;
        private double score;
    }

    public static void main(String[] args) {
        SumList sl = new SumList();
        sl.test01();
    }

    public void test01(){
        List<User> userList = getUsers();

        //求double总和
        double sumScore = userList.stream().mapToDouble(User::getScore).sum();
        System.out.println(sumScore);

        //求int总和
        int sumAge = userList.stream().mapToInt(User::getAge).sum();
        System.out.println(sumAge);

        //获取id最大的对象
        Optional<User> maxOptional = userList.stream().max(Comparator.comparing(User::getId));
        maxOptional.ifPresent((max)->{
            System.out.println(max.getId());
        });

        //获取id最小的对象
        Optional<User> minOptional = userList.stream().min(Comparator.comparing(User::getId));
        minOptional.ifPresent((min)->{
            System.out.println(min.getId());
        });
    }

    public List<User> getUsers(){
        List<User> list = new ArrayList<>();
        IntStream.range(1,11).forEach(i->{
            User  u = new User();
            u.setId(i);
            u.setName("张三"+i);
            u.setAge(15);
            u.setScore(60.5);

            list.add(u);
        });

        return list;
    }
}
